package com.telenav.comm;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;

import com.telenav.network.HttpConnection;

abstract class HttpStreamHandler
{
    public static boolean handle(HttpConnection httpConnection, RequestJob job)
    {
        OutputStream os = null;
        InputStream is = null;
        try
        {
            httpConnection.setRequestMethod(HttpConnection.POST);
            
            if (job.requestHeaders != null)
            {
                for (Enumeration em = job.requestHeaders.keys(); em.hasMoreElements(); )
                {
                    String header = (String) em.nextElement();
                    String value = (String) job.requestHeaders.get(header);
                    httpConnection.setRequestProperty(header, value);
                }
            }
            
            if (job.response.isCanceled)
                return false;

            os = httpConnection.openOutputStream();
            
            job.os = os;
            
            if(job.response.requestData != null)
            {
                os.write(job.response.requestData);
            }
        }
        catch (Throwable e)
        {   
            job.response.errorMessage = e.getMessage();
            job.response.exception = e;
            
            return false;
        }
        
        try
        {
            if (job.response.isCanceled)
                return false;
            
            int respCode = httpConnection.getResponseCode();
            
            if (respCode == HttpConnection.HTTP_OK)
            {
                is = httpConnection.openInputStream();
                job.is = is;
                
                try
                {                
                    boolean isSuccessful = readHttpContent(is, job);
                    
                    return isSuccessful;
                }
                catch (Throwable e)
                {
                    job.response.errorMessage = e.getMessage();
                    job.response.exception = e;
                    
                    return false;
                }
            }
            else
            {
                job.response.status = ICommCallback.RESPONSE_ERROR;
                job.response.errorMessage = httpConnection.getResponseMessage();
                
                return false;
            }
        }
        catch (Throwable e)
        {
            e.printStackTrace();
            
            job.response.errorMessage = e.getMessage();
            job.response.exception = e;
            
            return false;
        }
    }

    private static boolean readHttpContent(InputStream is, RequestJob job) throws Exception
    {
		if (job.response.isCanceled)
			return false;

		ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
		int ch;
		while ((ch = is.read()) != -1) {
			bytestream.write(ch);
		}
		bytestream.close();

		job.response.responseData = bytestream.toByteArray();

        if (job.commCallBack != null)
	    {
	    	job.commCallBack.handleChild(job.response);
	    }
        
        if (job.response.isCanceled)
            return false;
        
        return true;
    }
}
